package com.westar.core.controller;

import com.westar.base.cons.Constant;
import com.westar.base.emum.PublicServiceEnum;
import com.westar.base.exceptions.SelfHttpException;
import com.westar.base.pojo.WestarResult;
import com.westar.base.util.TokenUtil;
import com.westar.base.util.WestarHttpUtil;
import com.westar.core.conf.SysConst;
import com.westar.core.services.ImplementService;
import com.westar.core.services.UserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;

@RestController
@RequestMapping("/implement")
@Api("事项清单库查询")
public class ImplementController {

    private static Logger logger = LoggerFactory.getLogger(ImplementController.class);

    @Autowired
    private ImplementService implementService;

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private Environment env;


    @ApiOperation("同步事项库信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="token",dataType="String",required=true,value="token"),
            @ApiImplicitParam(paramType="query",name="areaCode",dataType="String",required=false,value="区域编码"),
            @ApiImplicitParam(paramType="query",name="pageNum",dataType="Integer",required=true,value="页码"),
            @ApiImplicitParam(paramType="query",name="pageSize",dataType="Integer",required=true,value="页数"),
            @ApiImplicitParam(paramType="query",name="showEventCode",dataType="String",required=false,value="事项编码"),
            @ApiImplicitParam(paramType="query",name="deptCodes",dataType="String",required=false,value="部门编码"),
            @ApiImplicitParam(paramType="query",name="isNotPublicServices",dataType="String",required=false,value="事项类型")
    })
    @RequestMapping(value = "/initImplementForDetailList", method = RequestMethod.GET, produces = "application/json")
    public WestarResult initImplement(String token,String areaCode, Integer pageNum,
                                      Integer pageSize,String showEventCode,String deptCodes
    ,String isNotPublicServices){

        if (StringUtils.isNotEmpty(SysConst.ITEM_NAME) && "suining".equals(SysConst.ITEM_NAME)) {
            String[] areaCodes = new String[]{"510900000000","510903000000","510904000000"
                    ,"510921000000","510922000000","510923000000","510906000000","510907000000"};
            if(!Arrays.asList(areaCodes).contains(areaCode)){
                return WestarResult.build( -1,"区域编码错误！仅支持【遂宁市】510900000000，【船山区】510903000000，" +
                        "【安居区】510904000000，【蓬溪县】510921000000，【射洪县】510922000000，【大英县】510923000000，" +
                        "【遂宁经济技术开发区】510906000000，【遂宁市河东新区管理委员会】510907000000" );
            }
        }

        if(StringUtils.isBlank(areaCode)){
            areaCode=SysConst.AREA_CODE;
        }
        if(StringUtils.isNotEmpty(isNotPublicServices)
                && null == PublicServiceEnum.getEnum(isNotPublicServices)){
            return WestarResult.build( -1,"isNotPublicServices：参数为power 、 service 或 空" );
        }
        if(StringUtils.isEmpty(isNotPublicServices)){
            isNotPublicServices = PublicServiceEnum.POWER.getParam();
        }

        try {
            String actCookie = TokenUtil.getCookie(token,Constant.MODEL_THINGSUI);
            return implementService.initImplementForDetailList(actCookie,areaCode,pageNum,pageSize,showEventCode,deptCodes,isNotPublicServices);
        } catch (IOException e) {
            return WestarResult.build( -1,"请求错误！" );
        } catch (SelfHttpException e) {
            return WestarResult.build( -1,"登录错误！" );
        }
    }
}
